#include"stdafx.h"
#include"permut.h"

int TPermut::SheduleN(void) {
    c = 0;
    int* Nr;
    int Ni;
    /**/
    Nc = N;
    for(int i = 0; i < n; i++ ,Nc++) {
        Nr = &N[RANDOM(n)];
        Ni = *Nc;
        *Nc = *Nr;
        *Nr =  Ni;
    }
    /**/
    Nc = N;
    return EXIT_SUCCESS;
}

TPermut::TPermut(int newn) {
    RANDOM_INIT();
    n = newn;
    N = new int[n];
    Nc = N;
    for(int i = 0; i < n; i++)
        *Nc++ = i;
    SheduleN();
    nNc = RANDOM(n);
    //nNc = 0;
}

TPermut::~TPermut(void) {
    delete [] N;
}

int TPermut::Get(void) {
    if(c == n)
        SheduleN();
    c++;
    return *Nc++;
}

int TPermut::GetStep(void) {
    oNc = nNc;
    nNc = Get();
    return (nNc - oNc);
}

int TPermut::ReCreate(int newn, bool first) {
    if (n != newn) {
        delete [] N;
        n = newn;
        N = new int[n];
        Nc = N;
        for(int i = 0; i < n; i++, Nc++)
            *Nc = i;
    }
    SheduleN();
    if(first)
        nNc = RANDOM(n);
        //nNc = 0;
    else
        nNc = *Nc;
    return nNc;
}
